﻿using ETask.Api.Dtos;
using ETask.Api.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace ETask.Api.Controllers
{
    [Route("api/user")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private IUserService _userService;
        private readonly IAuditLogService _auditLogService;

        public UserController(IUserService userService, IAuditLogService auditLogService)
        {
            _userService = userService;
            _auditLogService = auditLogService;
        }

        [HttpPost("getList")]
        public async Task<ApiResponse> GetList([FromBody] UserListParams listParams)
        {
            return await _userService.GetList(listParams);
        }

        [HttpPost("addUser")]
        public async Task<ApiResponse> AddUser([FromBody] CreateUserDto createUserDto)
        {
            try
            {
                await _userService.CreateAsync(createUserDto);
                
                // 记录用户创建日志
                await _auditLogService.LogSuccessAsync("创建", "用户", $"创建用户 {createUserDto.Name}", null, createUserDto.Name);
                
                return ApiResponse.Ok("添加成功");
            }
            catch (Exception ex)
            {
                await _auditLogService.LogErrorAsync("创建", "用户", $"创建用户 {createUserDto.Name} 失败", ex.Message, null, createUserDto.Name);
                return ApiResponse.Fail(500, $"添加失败: {ex.Message}");
            }
        }

        [HttpPost("updateUser")]
        public async Task<ApiResponse> UpdateUser([FromBody] UpdateUserDto updateUserDto)
        {
            try
            {
                await _userService.UpdateAsync(updateUserDto.Id, updateUserDto);
                
                // 记录用户修改日志
                await _auditLogService.LogSuccessAsync("修改", "用户", $"修改用户 {updateUserDto.Name}", updateUserDto.Id, updateUserDto.Name);
                
                return ApiResponse.Ok("修改成功");
            }
            catch (Exception ex)
            {
                await _auditLogService.LogErrorAsync("修改", "用户", $"修改用户 {updateUserDto.Name} 失败", ex.Message, updateUserDto.Id, updateUserDto.Name);
                return ApiResponse.Fail(500, $"修改失败: {ex.Message}");
            }
        }

        [HttpPost("delById")]
        public async Task<ApiResponse> DelById([FromBody] PrimaryKey key)
        {
            try
            {
                // 先获取用户信息用于日志记录
                var user = await _userService.GetByIdAsync(key.Id);
                var userName = user?.Name ?? "未知用户";
                
                await _userService.DeleteAsync(key.Id);
                
                // 记录用户删除日志
                await _auditLogService.LogSuccessAsync("删除", "用户", $"删除用户 {userName}", key.Id, userName);
                
                return ApiResponse.Ok("删除成功");
            }
            catch (Exception ex)
            {
                await _auditLogService.LogErrorAsync("删除", "用户", "删除用户失败", ex.Message, key.Id);
                return ApiResponse.Fail(500, $"删除失败: {ex.Message}");
            }
        }

        [HttpPost("resetUserPassword")]
        public async Task<ApiResponse> ResetUserPassword([FromBody] ResetPasswordDto resetPassword)
        {
            try
            {
                await _userService.ResetUserPassword(resetPassword);
                
                // 记录密码重置日志
                await _auditLogService.LogSuccessAsync("重置密码", "用户", $"重置用户密码", resetPassword.UserId);
                
                return ApiResponse.Ok("操作成功");
            }
            catch (Exception ex)
            {
                await _auditLogService.LogErrorAsync("重置密码", "用户", "重置用户密码失败", ex.Message, resetPassword.UserId);
                return ApiResponse.Fail(500, $"操作失败: {ex.Message}");
            }
        }
    }
}
